package fr.umlv.ig.metroApplication;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import fr.umlv.ig.event.LineEvent;
import fr.umlv.ig.event.StationEvent;
import fr.umlv.ig.event.TrainEvent;
import fr.umlv.ig.infoView.LineInfoView;
import fr.umlv.ig.infoView.StationInfoView;
import fr.umlv.ig.infoView.TrainInfoView;
import fr.umlv.ig.listener.LineListener;
import fr.umlv.ig.listener.StationAdapter;
import fr.umlv.ig.listener.TrainListener;
import fr.umlv.ig.metroFactory.GraphicFactory;
import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;
import fr.umlv.ig.model.Train;

/**
 * InfoPanel is class that will return
 * a jpanel whose contains the LineInfoView,
 * StationInfoView and the TrainInfoView
 * @author jenseric
 * @author william
 *
 */
public class InfoPanel {

	/**
	 * method that take a coordinate and return a string like (x,y)
	 * @param coordinate to take
	 * @return string like (x,y)
	 */
	public static String pointToString(Point2D coordinate) {
		StringBuilder coordinateString = new StringBuilder("Coordinates");
		coordinateString.append("(");
		coordinateString.append((int) coordinate.getX());
		coordinateString.append(",");
		coordinateString.append((int) coordinate.getY());
		coordinateString.append(")");
		return coordinateString.toString();
	}

	/**
	 * method that create the infoPanel
	 * @param metroModel use for getting data
	 * @param frame use for lineInfoView and StationInfoView
	 * @return
	 */
	public static JPanel getInfoPanel(final MetroModelImpl metroModel, final JFrame frame, ActionListener toolWindow) {
		JPanel infoPanel = new JPanel(new BorderLayout());
		infoPanel.setPreferredSize(new Dimension(300, 180));
		infoPanel.setName("Informations");
		final JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
		GraphicFactory.initLinesIcons();

		tabpane.setName("Informations ");
		final LineInfoView lineInfo = new LineInfoView(frame, metroModel, toolWindow);
		final StationInfoView stationInfo = new StationInfoView(frame, metroModel);
		final TrainInfoView trainInfo = new TrainInfoView(metroModel);

		tabpane.addTab("Line", lineInfo);
		tabpane.addTab("Station", stationInfo);
		tabpane.addTab("Train", trainInfo);

		metroModel.addLineListener(new LineListener() {	
			@Override
			public void lineChange(LineEvent e) {
				tabpane.setSelectedIndex(0);
				lineInfo.setInfo((Line) e.getSource());
			}
		});

		metroModel.addStationListener(new StationAdapter() {
			@Override
			public void stationChange(StationEvent e) {
				tabpane.setSelectedIndex(1);
				stationInfo.setInfo((Station) e.getSource(), frame);
			}
		});

		metroModel.addTrainListener(new TrainListener() {
			@Override
			public void trainChange(TrainEvent event) {

				tabpane.setSelectedIndex(2);
				trainInfo.setInfo(event);
				lineInfo.setInfo(metroModel.getLine((Train) event.getSource()));
			}
		});
		infoPanel.add(tabpane);
		return infoPanel;
	}
}
